//app.js
App({
  onLaunch: function () {

  },
  globalData: {
    userInfo: null,
    access_token:"",
  //  httpurl: "https://crm.haoxin168.cn/api/",
    httpurl: "http://127.0.0.1:8000/api/",
 //  httpurl: "https://testcrm.haoxin168.cn/api/",
    permission:[],
    isHomeXiaoFei: false, //消费首页
    isHomeNoXiaoFei: false, //没有消费首页
    isAddCustomer: false, //增加客户
    isAddXiaoFei: false,  //增加消费
    isCustomerManger: false, //客户管理
    isCustomerDistribution: false, //客户分配
    isCustomerTelephone: false, //客户拨打
    isCustomDetailsBoDa: false,//详情页拨打权限
    isCustomDetailsFenPei: false,//详情页拨打权限
    isMangerHome:false,//项目经理首页
    formids:"",
  },
  Login(){
    return new Promise((success, rej) => {
      wx.login({
        success: res => {
          console.log('小程序登录', res)
          if (res.code) {
            wx.request({
              url:  this.globalData.httpurl + 'login',
              data: { code: res.code },
              header: {},
              method: 'POST',
              dataType: 'json',
              responseType: 'text',
              success: res => {
                console.log(res)
                if (res.data.code === "success") {
                  this.globalData.access_token = res.data.data.access_token
                  this.globalData.userInfo = data.data
                  if (this.tokenReadyCallback) {
                    this.tokenReadyCallback(res)
                  }
                  success(res)
                }
              },
              fail: res => { rej() },
              complete: function (res) { },
            })
            //发起网络请求
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }


      })
    })
  },
  CheckSession(){
    if (this.globalData.access_token) {
      return new Promise((res, rej) => {
        wx.checkSession({
          success: () => {
            console.log('有token的检查')
            res(true)
          },
          fail: () => {
            // session_key 已经失效，需要重新执行登录流程
            wx.redirectTo({
              url: '/pages/index/index',
              success: function (res) { },
              fail: function (res) { },
              complete: function (res) { },
            })
          }
        })
      })
    } else {
      return new Promise((res, rej) => {
        this.tokenReadyCallback = success => {
          wx.checkSession({
            success: () => {
              res(true)
            },
            fail: () => {
              // session_key 已经失效，需要重新执行登录流程
              wx.redirectTo({
                url: '/pages/index/index',
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
              })
            }
          })
        }
      })
    }
  }
,
   



  GetUser() {

    //判断登录是否已经完成
    if (this.globalData.token) {
      return new Promise((res, ret) => {
        //判断user
        if (this.globalData.userInfo) {
          res(this.globalData.userInfo)
        } else {
          wx.request({
            url: this.globalData.httpurl + 'user',
            data: '',
            header: { 'Authorization': 'Bearer ' + this.globalData.token },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: data => {
              this.globalData.userInfo = data.data
              if (!data.data.mobile){
                wx.redirectTo({
                  url: '/pages/index/index',
                })
                return
              }
             
              res(data.data)
              console.log('有token', data.data)
             
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      })

    } else {
      return new Promise((res, ret) => {
        this.tokenReadyCallback = success => {
          if (this.globalData.userInfo) {
            res(this.globalData.userInfo)
          } else {
            wx.request({
              url: this.globalData.httpurl + 'user',
              data: '',
              header: { 'Authorization': 'Bearer ' + success.data.data.access_token },
              method: 'GET',
              dataType: 'json',
              responseType: 'text',
              success: data => {



                this.globalData.userInfo = data.data
                res(data.data)
                console.log('么有token', data.data)
                console.log(data)
              },
              fail: function (res) { },
              complete: function (res) { },
            })
          }
        }
      })

    }
  } ,  
  GetPermission(){
    wx.request({
      url: this.globalData.httpurl + 'permission',
      method: "GET",
      header: {
        "Authorization": "bearer " + this.globalData.access_token
      },
      data: {

      },
      success: function (res) {
        this.globalData.permission = [];
        var datas = res.data.data
        console.log(datas)
        for (var i = 0; i < datas.length; i++) {
          this.globalData.permission.push(datas[i].name)
          if (datas[i].name == 'home_xiaofei') {
            this.globalData.isHomeXiaoFei = true
          }
          if (datas[i].name == 'home_xiaofei') {
            this.globalData.isHomeNoXiaoFei = true
          }
          if (datas[i].name == 'customer_add') {
            this.globalData.isAddCustomer = true
          }
          if (datas[i].name == 'xiaofei_add') {
            this.globalData.isAddXiaoFei = true
          }
          if (datas[i].name == 'customer_manger') {
            this.globalData.isCustomerManger = true
          }
          if (datas[i].name == 'customer_distribution') {
            this.globalData.isCustomerDistribution = true
          }
          if (datas[i].name == 'customer_telephone') {
            this.globalData.isCustomerTelephone = true
          }
          if (datas[i].name == 'distribution') {
            this.globalData.isCustomDetailsFenPei = true
          }
          if (datas[i].name == 'callMobile') {
            this.globalData.isCustomDetailsBoDa = true
          }
          if (datas[i].name == 'manger_home') {
            this.globalData.isMangerHome = true
            console.log(222)
          }

        }
        console.log(this.globalData)
    
      }
    })
  }        



})